package com.parkinglot.core;

import com.alibaba.druid.util.StringUtils;
import com.parkinglot.util.RedisUtil;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class CustomPropertyConfig implements ApplicationContextAware {
    private static ApplicationContext context;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        context = applicationContext;
    }

    /**
     * 获取配置的属性
     *
     * @param key
     * @return
     */
    public static String getproperties(String key) {
        if (StringUtils.isEmpty(key)) {
            return null;
        }
        Environment environment = context.getEnvironment();
        return environment.getProperty(key);
    }

    /**
     * 初始化ZK配置的属性
     */
/*    @PostConstruct
    public void initZKConfig() {
        ZKListenerUtil.loadZKConfig();
    }*/

    /**
     * 初始化redisTemplate
     */
    @PostConstruct
    public void initRedisTemplate() {
        RedisUtil.setRedisTemplate((RedisTemplate<String, Object>) context.getBean("redisTemplate"));
    }
}
